import { createHtmlPlugin } from 'vite-plugin-html';

export const configHtmlPlugin = (env) => {
  const {
    VITE_APP_NODE_ENV: appEnv,
    VITE_APP_NAME: appName,
    VITE_APP_NAME_ZH: appNameZh,
    VITE_APP_ROOT: appRoot,
    VITE_APP_PUBLIC_DIR: appPublicDir,
    VITE_APP_LOGO_DIR: appFaviconDir,
  } = env;
  return createHtmlPlugin({
    minify: true,
    inject: {
      data: {
        // 出现在模版中的 <%- title %>
        title: appNameZh,
        faviconDir: appFaviconDir,
        injectStyle: `<style></style>`,
        injectScript: `
          <script>
            window.$_vite_app_env = '${appEnv}';
            window.$_vite_app_name = '${appName}';
            window.$_vite_app_name_zh = '${appNameZh}';
            window.$_vite_app_root = '${appRoot}';
            window.$_vite_app_public_dir = '${appPublicDir}';
          </script>
        `,
      },
    },
  });
};
